package com.hxkj.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.TagsClassifyDao;
import com.hxkj.dao.TagsDao;
import com.hxkj.domain.po.Tags;
import com.hxkj.domain.po.TagsClassify;
import com.hxkj.domain.enums.TagsClassifyExceptionEnum;
import com.hxkj.service.TagsClassifyService;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TagsClassifyServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/7 12:01
 * @Version 1.0
 */

@Service
@Slf4j
public class TagsClassifyServiceImpl implements TagsClassifyService {

    @Autowired
    TagsClassifyDao tagsClassifyDao;

    @Autowired
    TagsDao tagsDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct saveTagsClassify(TagsClassify tagsClassify) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:判断名称是否已存在
            if (tagsClassifyDao.getTagsClassifyByName(tagsClassify.getClassifyName(), tagsClassify.getSiteId()).size() != 0) {
                resultMessageStruct.setCode(TagsClassifyExceptionEnum.TAGS_CLASSIFY_IS_EXIST.getCode());
                resultMessageStruct.setMessage(TagsClassifyExceptionEnum.TAGS_CLASSIFY_IS_EXIST.getMsg());
                return resultMessageStruct;
            }
            tagsClassifyDao.saveTagsClassify(tagsClassify);
            resultMessageStruct.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsClassifyExceptionEnum.TAGS_CLASSIFY_SAVE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsClassifyExceptionEnum.TAGS_CLASSIFY_SAVE_ERROE.getMsg());
            throw e;
        }

        return resultMessageStruct;
    }

    @Override
    public PageInfo<TagsClassify> getTagsClassifyList(TagsClassify tagsClassify, PageTo pageTo) {
        Page<TagsClassify> page = PageHelper.startPage(pageTo.getPageNum(), pageTo.getPageSize());
        PageInfo<TagsClassify> pageInfo = new PageInfo<>();
        List<TagsClassify> tagsClassifyList;
        try {
            tagsClassifyList = tagsClassifyDao.getTagsClassifyList(tagsClassify);
            pageInfo = new PageInfo<>(page);
            pageInfo.setList(tagsClassifyList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签分类列表失败");
        }
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct removeTagsClassify(TagsClassify tagsClassify) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            //Mr.zheng:切割id字符串
            String[] tagsClassifyIds = tagsClassify.getTagsClassifyIds().split(",");
            //Mr.zheng:遍历删除
            for (int i = 0; i < tagsClassifyIds.length; i++) {
                Long tagsClassifyId = Long.valueOf(tagsClassifyIds[i]);
                //Mr.zheng:将关联的tags标签置于未分类
                modifyTagsClassifyId(tagsClassifyId);
                tagsClassifyDao.removeTagsClassifyById(tagsClassifyId);
            }
            resultMessageStruct.setCode(200);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsClassifyExceptionEnum.TAGS_CLASSIFY_DELETE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsClassifyExceptionEnum.TAGS_CLASSIFY_DELETE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct modifyTagsClassify(TagsClassify tagsClassify) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            TagsClassify tagc = tagsClassifyDao.getTagsClassifyById(tagsClassify.getTagsClassifyId());
            //Mr.zheng:查看标签名称是否修改
            if (tagc.getClassifyName().equals(tagsClassify.getClassifyName())) {
                tagsClassifyDao.modifyTagsClassify(tagsClassify);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
            //Mr.zheng:查看名字是否重复
            if (tagsClassifyDao.getTagsClassifyByName(tagsClassify.getClassifyName(), tagsClassify.getSiteId()).size() == 0) {
                tagsClassifyDao.modifyTagsClassify(tagsClassify);
                resultMessageStruct.setCode(200);
                return resultMessageStruct;
            }
            resultMessageStruct.setCode(TagsClassifyExceptionEnum.TAGS_CLASSIFY_IS_EXIST.getCode());
            resultMessageStruct.setMessage(TagsClassifyExceptionEnum.TAGS_CLASSIFY_IS_EXIST.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(TagsClassifyExceptionEnum.TAGS_CLASSIFY_UPDATE_ERROE.getCode());
            resultMessageStruct.setMessage(TagsClassifyExceptionEnum.TAGS_CLASSIFY_UPDATE_ERROE.getMsg());
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public List<TagsClassify> getTagsClassifyDownList(TagsClassify tagsClassify) {
        List<TagsClassify> tagsClassifyList = new ArrayList<>();
        try {
            tagsClassifyList = tagsClassifyDao.getTagsClassifyDownList(tagsClassify);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取标签分类列表失败");
        }
        return tagsClassifyList;
    }

    //Mr.zheng:将关联的tags标签置于未分类
    private void modifyTagsClassifyId(Long tagsClassifyId) {
        List<Tags> tagsList = tagsDao.getTagsListByTagsClassifyId(tagsClassifyId);
        Tags tags = new Tags();
        tags.setTagsClassifyId(0L);
        for (Tags tag : tagsList) {
            tags.setTagsId(tag.getTagsId());
            tagsDao.modifyTags(tags);
        }
    }
}
